package com.fjb.controller.brand;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.brand.BrandStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.common.plugins.ZTree;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.brand.query.BrandInfoQuery;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.pojo.product.ProductCategory;
import com.fjb.pojo.user.SysUser;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductCategoryService;

/**
 * <p>
 * 品牌信息 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/brandInfo")
public class BrandInfoController extends BaseController{
	
	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	 * @Description:显示分页列表
	 * @param request
	 * @return
	 * ModelAndView
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午5:58:26
	 */
	@RequestMapping(value="/showPageList")
    public ModelAndView showPageList(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/brand/brand_info_list");
    	mv.addObject(FILE_SERVICE_LOCATION, GlobalConfig.FILE_SERVICE_LOCATION);
    	return mv;
    }
	
	/**
	 * @Description:分页列表查询
	 * @param request
	 * @param pageInfoQuery
	 * @return
	 * JsonPageResult<List<BrandInfo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午6:08:47
	 */
	@RequestMapping(value = "/selectPageList")
    @ResponseBody
    public JsonTotalResult<List<BrandInfo>> selectPageList(HttpServletRequest request,BrandInfoQuery brandInfoQuery
    		, PageQuery pageQuery){
        return brandInfoService.selectPageList(brandInfoQuery,pageQuery);
    }
	
	/**
	 * @Description:查询树形列表
	 * @param request
	 * @return
	 * JsonResult<List<ZTree>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月4日 下午8:55:19
	 */
	@RequestMapping(value = "/selectZTreeList")
    @ResponseBody
    public JsonResult<List<ZTree>> selectZTreeList(HttpServletRequest request){
		QueryWrapper<BrandInfo> queryWrapper = new QueryWrapper<BrandInfo>();
		queryWrapper.orderByAsc("sort");
		List<BrandInfo> infoList = brandInfoService.list(queryWrapper);
		ArrayList<ZTree> zTreeList = new ArrayList<ZTree>();
		if(infoList.size()>0) {
			for (BrandInfo brandInfo : infoList) {
				Integer id = brandInfo.getId();
				String name = brandInfo.getBrandName();
				Integer sort = brandInfo.getSort();
				ZTree zTree = new ZTree();	
				zTree.setId(id);
				zTree.setpId(0);		
				zTree.setName(name);
				zTree.setSort(sort);
				zTreeList.add(zTree);
			}
		}
		return new JsonResult<List<ZTree>>(zTreeList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:添加
	 * @param request
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月5日 下午8:39:27
	 */
	@RequestMapping(value = "/addInfo")
    @ResponseBody
    public JsonResult<Boolean> addInfo(HttpServletRequest request,Integer sort){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		String brandName = request.getParameter("brandName");
		if(StringUtils.isBlank(brandName)) {
			httpCode.setMsg("品牌名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String initial = request.getParameter("initial");
		if(StringUtils.isBlank(initial)) {
			httpCode.setMsg("首字母不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String description = request.getParameter("description");
		if(StringUtils.isBlank(description)) {
			httpCode.setMsg("说明不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(sort==null) {
			httpCode.setMsg("排序不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
		
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandName(brandName);
		brandInfo.setInitial(initial);
		brandInfo.setSort(sort);
		brandInfo.setDescription(description);
		brandInfo.setMainUserId(mainUserId);
		brandInfo.setCreateUserId(userId);	
		brandInfo.setCreateTime(LocalDateTime.now());
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		boolean b = brandInfoService.save(brandInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	
	/**
	 * @Description:修改
	 * @param request
	 * @param brandId
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月5日 下午8:39:39
	 */
	@RequestMapping(value = "/updateInfo")
    @ResponseBody
    public JsonResult<Boolean> updateInfo(HttpServletRequest request,Integer brandId,Integer sort){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		String brandName = request.getParameter("brandName");
		if(StringUtils.isBlank(brandName)) {
			httpCode.setMsg("品牌名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String initial = request.getParameter("initial");
		if(StringUtils.isBlank(initial)) {
			httpCode.setMsg("首字母不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String description = request.getParameter("description");
		if(StringUtils.isBlank(description)) {
			httpCode.setMsg("说明不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(sort==null) {
			httpCode.setMsg("排序不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
		
		QueryWrapper<BrandInfo> queryWrapper = new QueryWrapper<BrandInfo>();
		queryWrapper.eq("id", brandId);
		queryWrapper.eq("main_user_id", mainUserId);
		BrandInfo oldBrandInfo = brandInfoService.getOne(queryWrapper);
		if(oldBrandInfo==null) {
			httpCode.setMsg("品牌信息不存在");
			return new JsonResult<Boolean>(null, httpCode);
		}
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setId(brandId);
		brandInfo.setBrandName(brandName);
		brandInfo.setInitial(initial);
		brandInfo.setSort(sort);
		brandInfo.setDescription(description);
		brandInfo.setMainUserId(mainUserId);
		brandInfo.setUpdateTime(LocalDateTime.now());
		brandInfo.setUpdateUserId(userId);
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		boolean b = brandInfoService.updateById(brandInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:根据id查询
	 * @param request
	 * @param brandId
	 * @return
	 * JsonResult<BrandInfo>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午1:33:15
	 */
	@RequestMapping(value = "/getById")
    @ResponseBody
    public JsonResult<BrandInfo> getById(HttpServletRequest request,Integer brandId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null) {	
			return new JsonResult<BrandInfo>(null, httpCode);
		}
		BrandInfo brandInfo = brandInfoService.getById(brandId);
		return new JsonResult<BrandInfo>(brandInfo, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:获得品牌列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午1:48:07
	 */	
	@RequestMapping(value = "/selectList")
    @ResponseBody
    public JsonResult<List<BrandInfo>> selectList(HttpServletRequest request){
		QueryWrapper<BrandInfo> queryWrapper = new QueryWrapper<BrandInfo>();
		List<BrandInfo> list = brandInfoService.list(queryWrapper);
        return new JsonResult<List<BrandInfo>>(list, HttpCode.SUCCESS);
    }
	
	/**
	 * @Description:查询正常的品牌列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月18日 下午4:44:41
	 */
	@RequestMapping(value = "/selectNormalList")
    @ResponseBody
    public JsonResult<List<BrandInfo>> selectNormalList(HttpServletRequest request){
		QueryWrapper<BrandInfo> queryWrapper = new QueryWrapper<BrandInfo>();
		queryWrapper.eq("brand_status", BrandStatus.STATUS_1.getCode());
		List<BrandInfo> list = brandInfoService.list(queryWrapper);
        return new JsonResult<List<BrandInfo>>(list, HttpCode.SUCCESS);
    }
	
	/**
	 * @Description:查询品牌车型树形列表
	 * @param request
	 * @return
	 * JsonResult<List<ZTree>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月30日 下午6:49:20
	 */
	@RequestMapping(value = "/selectCategoryZTreeList")
    @ResponseBody
    public JsonResult<List<ZTree>> selectCategoryZTreeList(HttpServletRequest request){
		// 查询品牌
		QueryWrapper<BrandInfo> queryWrapper = new QueryWrapper<BrandInfo>();
		queryWrapper.orderByAsc("sort");
		List<BrandInfo> infoList = brandInfoService.list(queryWrapper);
		
		// 查询车型
		QueryWrapper<ProductCategory> queryWrapperCategory = new QueryWrapper<ProductCategory>();
		queryWrapperCategory.eq("category_level", 1);
		List<ProductCategory> categoryList = productCategoryService.list(queryWrapperCategory);
		List<ZTree> zTreeList = new ArrayList<ZTree>();
		if(infoList.size()>0) {
			for (BrandInfo brandInfo : infoList) {
				Integer id = brandInfo.getId();
				String name = brandInfo.getBrandName();
				Integer sort = brandInfo.getSort();
				Integer brandStatus = brandInfo.getBrandStatus();
				if(brandStatus.equals(BrandStatus.STATUS_2.getCode())) {
					name = name + "-" +BrandStatus.STATUS_2.getMsg();
				}
				ZTree zTree = new ZTree();	
				zTree.setId(id);
				zTree.setpId(0);		
				zTree.setName(name);
				zTree.setSort(sort);
				zTreeList.add(zTree);
			}
		}
		/*List<ZTree> pcZTreeList = new ArrayList<ZTree>();
		for (ZTree zTree : zTreeList) {
			Integer id = zTree.getId();
			for (ProductCategory pc : categoryList) {
				Integer brandId = pc.getBrandId();
				if(id.equals(brandId)) {
					Integer pcId = pc.getId();
					String categoryName = pc.getCategoryName();
					ZTree pcZTree = new ZTree();	
					pcZTree.setId(pcId);
					pcZTree.setpId(brandId);		
					pcZTree.setName(categoryName);
					pcZTreeList.add(pcZTree);	
				}
			}
		}	
		zTreeList.addAll(pcZTreeList);*/
		return new JsonResult<List<ZTree>>(zTreeList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改品牌状态
	 * @param request
	 * @param brandId
	 * @param brandStatus
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年3月2日 下午8:22:02
	 */
	@RequestMapping(value = "/updateStatus")
    @ResponseBody
    public JsonResult<Boolean> updateStatus(HttpServletRequest request,Integer brandId,Integer brandStatus){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null||brandStatus==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		BrandInfo bi = new BrandInfo();
		bi.setId(brandId);
		bi.setBrandStatus(brandStatus);
		bi.setUpdateUserId(userId);
		bi.setUpdateTime(LocalDateTime.now());
		boolean b = brandInfoService.updateById(bi);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改热门开关
	 * @param request
	 * @param brandId
	 * @param hotSwitch
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao	
	 * @time:2020年3月4日 下午7:55:28
	 */
	@RequestMapping(value = "/updateHotSwitch")
    @ResponseBody
    public JsonResult<Boolean> updateHotSwitch(HttpServletRequest request,Integer brandId,Boolean switchs){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null||switchs==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}	
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		BrandInfo bi = new BrandInfo();
		bi.setId(brandId);
		if(switchs==true) {
			bi.setHotSwitch(1);
		}else {	
			bi.setHotSwitch(2);
		}
		bi.setUpdateUserId(userId);
		bi.setUpdateTime(LocalDateTime.now());
		boolean b = brandInfoService.updateById(bi);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
}
